global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/figure_A10_alm_scatterplots.log, replace name(fig)

capture noi {
    * ---------------------------------
    * Output the scatterplots
    * ---------------------------------

    clear
    global indvers "ind6090"
    global cvers "_ipc4" 
    local min_pats 0 
    if (`min_pats' != 0) local restr "_min`min_pats'pats"

    use ${final_dir}/ind6090_patents_regression_data${cvers}, clear

    tostring ind6090, replace
    gen sh_auto95_use_shifted = sh_auto95_use + 0.0005

    cap program drop flag_outliers
    program flag_outliers
        cap drop resid ind6090_outl
        predict resid, residuals
        sort resid
        gen ind6090_outl1 = " "
        replace ind6090_outl = ind6090 if _n <= 5
        gsort -resid
        replace ind6090_outl = ind6090 if _n <= 5
        sort ind6090
    end


    keep if in_relevant_field_use  >= `min_pats'

    * Figure template
    qui include ${code_dir}/config/figuretools.do

    local ind_symb "mcol(%30) mlw(thin)"
    local ind_lab "mlab(ind6090_outl) mlabsize(vsmall) mlabc(gs8) msym(i)"
    local ind_lab2 "mcol(%30) mlw(thin)" 
    local legend "legend(lab(1 "Industries") lab(3 "") lab(2 "Linear fit") rows(1))"

    * A: Routine cognitive
    reg dqsts sh_auto95_use [aw=avwt], vce(cluster uas_ind6090)
    local coef1 : di %3.1f _b[sh_auto95_use]
    local se1 : di %3.1f _se[sh_auto95_use]
    corr dqsts sh_auto95_use [aw=avwt]
    local corr1 : di %3.2f el(r(C),1,2)

    flag_outliers


    tw (scatter dqsts sh_auto95_use [aw=avwt], `ind_symb' ///
    ytitle("Δ Routine cognitive") xtitle("Share automation") ylab(#4)) ///
    (scatter dqsts sh_auto95_use_shifted, `ind_lab') ///
    || lfit dqsts sh_auto95_use [aw=avwt], text(-18.5 0.11 "Coef. `coef1' (`se1')" "Corr.    `corr1'", color(gs5) just(left)) lcolor(gs4) lwidth(medthick) ///
    `legend' ///
    name(p1, replace) title("(A)  Δ Routine cognitive", position(6))
    graph export ${fig_dir}/appendix/Figure_A10_alm_scatterplot_a`restr'${cvers}.pdf, replace
    graph export ${fig_dir}/appendix/Figure_A10_alm_scatterplot_a`restr'${cvers}.eps, replace

    * B: Routine manual
    reg dqfing sh_auto95_use [aw=avwt], vce(cluster uas_ind6090)
    local coef2 : di %3.1f _b[sh_auto95_use]
    local se2 : di %3.1f _se[sh_auto95_use]
    corr dqfing sh_auto95_use [aw=avwt]
    local corr2 : di %3.2f el(r(C),1,2)

    flag_outliers

    tw (scatter dqfing sh_auto95_use [aw=avwt], `ind_symb' ///
    ytitle("Δ Routine manual") xtitle("Share automation") ylab(#4))  ///
    (scatter dqfing sh_auto95_use_shifted, `ind_lab') ///
    || lfit dqfing sh_auto95_use [aw=avwt], text(-18.9 0.11 "Coef. `coef2' (`se2')" "Corr.    `corr2'", color(gs5) just(left)) lcolor(gs4) lwidth(medthick) ///
    `legend' ///
    name(p2, replace) title("(B)  Δ Routine manual", position(6))
    graph export ${fig_dir}/appendix/Figure_A10_alm_scatterplot_b`restr'${cvers}.pdf, replace
    graph export ${fig_dir}/appendix/Figure_A10_alm_scatterplot_b`restr'${cvers}.eps, replace


    * C: High/Low Skill Ratio of Workers

    reg dratioHL sh_auto95_use [aw=avwt], vce(cluster uas_ind6090)
    local coef3 : di %3.1f _b[sh_auto95_use]
    local se3 : di %3.1f _se[sh_auto95_use]
    corr dratioHL sh_auto95_use [aw=avwt]
    local corr3 : di %3.2f el(r(C),1,2)

    flag_outliers


    tw (scatter dratioHL sh_auto95_use [aw=avwt], `ind_symb' ///
    ytitle("Δ High / low skill workers") xtitle("Share automation") yscale(r(-0.7,1.25)) ylab(#4)) ///
    (scatter dratioHL sh_auto95_use_shifted, `ind_lab') ///
    || lfit dratioHL sh_auto95_use [aw=avwt], text(-0.50 0.11 "Coef.  `coef3' (`se3')" "Corr.  `corr3'", color(gs5) just(left)) lcolor(gs4) lwidth(medthick) ///
    `legend' ///
    name(p3, replace) title("(C)  Δ High / low skill workers", position(6))

    graph export ${fig_dir}/appendix/Figure_A10_alm_scatterplot_c`restr'${cvers}.pdf, replace
    graph export ${fig_dir}/appendix/Figure_A10_alm_scatterplot_c`restr'${cvers}.eps, replace

    * D: Labor share NBER

    reg d_lshare_nber sh_auto95_use [aw=avwt], vce(cluster uas_ind6090)
    local coef4 : di %3.1f _b[sh_auto95_use]
    local se4 : di %3.1f _se[sh_auto95_use]
    corr dratioHL sh_auto95_use [aw=avwt]
    local corr4 : di %3.2f el(r(C),1,2)

    flag_outliers

    tw (scatter d_lshare_nber sh_auto95_use [aw=avwt], `ind_symb' ///
    ytitle("Δ Labor share") xtitle("Share automation") ylab(#4))  ///
    (scatter d_lshare_nber sh_auto95_use_shifted, `ind_lab') ///
    || lfit d_lshare_nber sh_auto95_use [aw=avwt], text(-0.20 0.11 "Coef.  `coef4' (`se4')" "Corr.   `corr4'", color(gs5) just(left)) lcolor(gs4) lwidth(medthick) ///
    `legend' ///
    name(p4, replace) title("(D)  Δ Labor share (NBER)", position(6))

    graph export ${fig_dir}/appendix/Figure_A10_alm_scatterplot_d`restr'${cvers}.pdf, replace
    graph export ${fig_dir}/appendix/Figure_A10_alm_scatterplot_d`restr'${cvers}.eps, replace

    * E: Labor share All

    local ind_lab "msymb(circle_hollow) mcol(gs6) mlw(thin) mlab(beaind) mlabsize(vsmall) mlabc(gs8)"

    use ${final_dir}/beaind_patents_regression_data${cvers}.dta, clear
    keep if in_relevant_field_use  >= `min_pats'
    tostring beaind, replace
    gen sh_auto95_use_shifted = sh_auto95_use + 0.0005

    reg d_lshare sh_auto95_use [aw=avwt], vce(cluster uas_beaind)
    local coef5 : di %3.1f _b[sh_auto95_use]
    local se5 : di %3.1f _se[sh_auto95_use]
    corr dratioHL sh_auto95_use [aw=avwt]
    local corr5 : di %3.2f el(r(C),1,2)

    cap drop resid beaind_outl
    predict resid, residuals
    sort resid
    gen beaind_outl1 = " "
    replace beaind_outl = beaind if _n <= 5
    gsort -resid
    replace beaind_outl = beaind if _n <= 5
    sort beaind

    local ind_lab "mlab(beaind_outl) mlabsize(vsmall) mlabc(gs8) msym(i)"

    tw (scatter d_lshare sh_auto95_use [aw=avwt], `ind_symb' ///
    ytitle("Δ Labor share") xtitle("Share automation") ylab(#4)) ///
    (scatter d_lshare sh_auto95_use_shifted, `ind_lab') ///
    || lfit d_lshare sh_auto95_use [aw=avwt], text(-0.53 0.11 "Coef.  `coef5' (`se5')" "Corr.   `corr5'", color(gs5) just(left)) lcolor(gs4) lwidth(medthick) ///
    `legend' ///
    name(p5, replace) title("(E)  Δ Labor share (BEA)", position(6))

    graph export ${fig_dir}/appendix/Figure_A10_alm_scatterplot_e`restr'${cvers}.pdf, replace
    graph export ${fig_dir}/appendix/Figure_A10_alm_scatterplot_e`restr'${cvers}.eps, replace

    grc1leg2 p1 p2 p3 p4 p5, iscale(*0.8) xsize(12) ysize(7) mtsize(small)

    graph export ${fig_dir}/appendix/Figure_A10_alm_scatterplots_combined.pdf, replace
    graph export ${fig_dir}/appendix/Figure_A10_alm_scatterplots_combined.eps, replace

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close fig